<?php
namespace App\Repositories;

use App\Models\Video;
use App\Models\VideoLearn;

class VideoLearnRepository extends BaseRepository
{
    public function __construct(
        VideoLearn $video_learn
    )
    {
        $this->model = $video_learn;
    }


    /**
     * 获取理论学习列表
     * @param array $data
     * @param array|string $extra
     * @param string $size
     * @return string
     */
    public function index($data, $student_id, $size = '')
    {

    }


    /**
     * 存储odb
     * @param $inputs
     */
    public function store($inputs, $extra = '')
    {

        $v_pid = Video::where('id', $inputs['video_id'])->first()->parentid;
        $km = $v_pid == '281' ? '1' : '4';
        //视频学习记录-

        $inputs['km'] = $km;
        $id = $this->model->insertGetId($inputs);

        //学时培训记录
        $time = time();
        $putime_log = $inputs;
        $putime_log['endtime'] = $time;
        $putime_log['km'] = $km;
        $putime_log['startime'] = $time - $inputs['duration']; //满足以前的畸形命名
        $putime_log['photo1'] = 'http://image.stjp360.com/theory_photo/'.$inputs['student_id'].'_'.$inputs['video_id'].'_1.jpg';
        $putime_log['photo2'] = 'http://image.stjp360.com/theory_photo/'.$inputs['student_id'].'_'.$inputs['video_id'].'_2.jpg';
        $putime_log['date'] = $time;
        $putime_log['sign'] = strtoupper(md5($time.'klbHLNnokLIHlHILHhl'));
        $putime_log['video_learn_id'] = $id;

        $ch = curl_init(env('THEROY_POST_URL'));

        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $putime_log);
        $body = curl_exec($ch);

        $tmp = json_decode($body, true);
        return true;
    }


    /**
     * 更新
     * @param $id
     * @param $input
     * @return mixed
     */
    public function update($id, $input,$extra = '')
    {
        $model = $this->getById($id);
        return $this->save($model,$input);
    }


    /**
     * 编辑特定id资源
     * @param  int $id 资源id
     * @param  string|array $extra 可选额外传入的参数
     * @return Illuminate\Support\Collection
     */
    public function edit($id, $extra)
    {

    }

    /**
     * @param $model
     * @param $inputs
     * @param bool $falg
     * @return mixed
     */
    public function save($model , $inputs , $falg = false)
    {
        return $model;
    }

}